package com.example.weather_news.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.example.weather_news.R;
import com.example.weather_news.adapter.Adapter_Future;
import com.example.weather_news.http.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class WeatherFutureActivity extends AppCompatActivity {

    private ImageView iv_future_back;
    private ListView lv_future;

    private Adapter_Future adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_future);
        initView();
        adapter=new Adapter_Future(WeatherFutureActivity.this);
        lv_future.setAdapter(adapter);

        iv_future_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            }
        });

        String cityName=this.getIntent().getStringExtra("cityName");
        queryFutureWeather(cityName);
    }

    //查询未来7天天气
    private void queryFutureWeather(String cityName) {
        HttpRequest.requestWeather(cityName, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.getInt("status") != 0) {
                        Toast.makeText(WeatherFutureActivity.this, jsonObject.getString("msg"), Toast
                                .LENGTH_SHORT).show();
                        return;
                    }
                    String result = jsonObject.getString("result");
                    JSONObject jo = new JSONObject(result);

                    String daily=jo.getString("daily");
                    JSONArray jsonArray=new JSONArray(daily);

                    adapter.setData(jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },null);
    }

    private void initView() {
        iv_future_back = (ImageView) findViewById(R.id.iv_future_back);
        lv_future = (ListView) findViewById(R.id.lv_future);
    }
}
